// pages/dome/dome.js
const api = require('../../utils/http.js');
const app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
    messageNum:0,
    isHidden:false,
		accountInfo: {},
		isRider: false,
    isDistribution: null,
		userOrder: [{
			"type": 1,
			"status": "待付款",
			"bage": "2",
			"icon": "../images/userOrder_icon_01.png"
		}, {
			"type": 2,
			"status": "待接单",
			"bage": "11",
			"icon": "../images/userOrder_icon_02.png"
		}, {
			"type": 3,
			"status": "待送达",
			"bage": "11",
			"icon": "../images/userOrder_icon_03.png"
		}, {
			"type": 5,
			"status": "退款",
			"bage": "",
			"icon": "../images/userOrder_icon_04.png"
		}]
	},

  //我的消息
  toMessage(){
    wx.navigateTo({
      url: "/pages/myMessage/myMessage"
    })
  },

  //热门评论
  toHotDynamic(){
    wx.navigateTo({
      url: "/pages/dynamicList/dynamicList"
    })
  },

  //授权回调
  toAuthor(e){
    console.log(e)
    if (e.detail.errMsg =='getUserInfo:ok'){
      wx.reLaunch({
        url: '/pages/index/index?scene=' + app.globalData.xid,
      })
    }else{
      this.setData({
        isHidden: false
      })
      return
    }
   
  },
  cancel() {
    this.setData({
      isHidden: false
    })
  },

	toLogin() {
		// wx.showModal({
		// 	title: '您还未登录',
		// 	content: '请先登录再进行操作',
		// 	success: (res) => {
		// 		if (res.confirm) {
		// 			this.isRiderAjax()
		// 		} else if (res.cancel) {
		// 			console.log('用户点击取消')
		// 		}
		// 	}
		// })
    this.setData({
      isHidden:true
    })
	},
	//加入我们
	addUs: function() {
    if(!app.globalData.uid){
      this.setData({
        isHidden: true
      })
      return
    }
		wx.navigateTo({
			url: '/pages/certifiedRider/certifiedRider'
		})
	},
	// 更多订单
	moreOrder: function() {
		wx.navigateTo({
			url: '/pages/MoreOrder/MoreOrder'
		})
	},
	//发布订单
	toReleaseOrder: function(e) {
		console.log(e.currentTarget.id)
		let typeId = e.currentTarget.id
		wx.navigateTo({
			url: '/pages/releaseOrder/releaseOrder?id=' + typeId
		})
	},
	//
	toRobOrder: function(e) {
		let typeId = e.currentTarget.dataset.id
		wx.navigateTo({
			url: '/pages/robOrder/robOrder?id=' + typeId
		})
	},
	distributionCenter: function() {
		wx.navigateTo({
			url: '/pages/distributionCenter/distributionCenter'
		})
	},
	// 等级礼包
	gift: function() {
		wx.navigateTo({
			url: '/pages/gradeGift/gradeGift'
		})
	},
	// 地址管理
	address: function() {
		wx.navigateTo({
			url: '/pages/goodsAddress/goodsAddress'
		})
	},

  //红包领取入口
  toHongbao(){
    wx.navigateTo({
      url: '/pages/couponCenter/couponCenter'
    })
  },
	// 客服
	customer: function() {
		// wx.navigateTo({
		// 	url: '/pages/contactCustomer/contactCustomer'
		// })
	},
	// 金币
	goMyGold() {
		let gold = this.data.accountInfo.jinbi ? this.data.accountInfo.jinbi : 0
		wx.navigateTo({
			url: '/pages/myGold/myGold?gold=' + gold
		})
	},
	// 余额
	goBalance() {
		let balance = this.data.accountInfo.zhanghu ? this.data.accountInfo.zhanghu : 0;
    let jinbi = this.data.accountInfo.jinbi ? this.data.accountInfo.jinbi : 0
		wx.navigateTo({
      url: '/pages/balance/balance?balance=' + balance + '&jinbi=' + jinbi
		})
	},

	//跳转优惠券
	toCoupon() {
		wx.navigateTo({
			url: '../coupon/coupon',
		})
	},
	//退款
	refund() {
		console.log(111)
	},
	isRiderAjax() {
		let uid = app.globalData.uid;
		api.fetchApi('procedure_t', {
			p: `R2002008|${uid}`
		}).then(res => {
			const { code, results } = res.data
			if (code == 1) {
				let isRiderBoolean = results[0].renzheng == 0 ? false : true
        let isDistribution = results[0].chief_permission == 0 ? false : true
        //把用户身份存全局 防止后台瞬间审核首页不刷新
        app.globalData.renzheng = results[0].renzheng
        //console.log(app.globalData.renzheng,'====')
				this.setData({
					isRider: isRiderBoolean,
					accountInfo: results[0],
          isDistribution
				})
			} else {
				wx.showToast({
					title: `${res.data.errorMsg}`,
					icon: 'none'
				})
			}
		})
	},

  //跳转签到页面
  toSignIn() {
    wx.navigateTo({
      url: "../signIn/signIn"
    })
  },

//查询消息数量
getMessageNum(){
  let uid = app.globalData.uid;
  api.fetchApi('procedure_t', { p: `R2018010|${uid}`}).then(res=>{
    if(res.data.code==1){
      var messageNum = res.data.results[0].total;
      this.setData({
        messageNum
      })
    }
  })
},
//编辑个人资料
  editProfile(){
    wx.navigateTo({
      url: "/pages/editProfile/editProfile"
    })
  },
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
    this.setData({
      cj: app.globalData.cj || ''
    })
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		if (app.globalData.uid) {
			this.isRiderAjax();
      this.getMessageNum();
		}
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
